package com.zian.controller.admin;


import com.zian.annotation.OperLog;
import com.zian.common.R;
import com.zian.domain.dto.OrderCancelDTO;
import com.zian.domain.dto.OrderStatusDTO;
import com.zian.domain.dto.PageDTO;
import com.zian.domain.po.SysOrder;
import com.zian.domain.query.OrderQuery;
import com.zian.domain.vo.OrderDetailsVO;
import com.zian.domain.vo.StatisticsOrderCountByStatus;
import com.zian.enums.BusinessType;
import com.zian.service.ISysOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author
 * @since 2024-09-08
 */
@Api("订单管理")
@Slf4j
@RestController("SysOrderControllerAdmin")
@RequestMapping("/admin/orders")
@RequiredArgsConstructor
public class SysOrderController {

    private final ISysOrderService orderService;

    @GetMapping("/page")
    @ApiOperation("订单列表")
    public R<PageDTO<SysOrder>> orderQuery(OrderQuery query) {
        log.info("queryOrderList:{}", query);
        PageDTO<SysOrder> vos = orderService.orderQuery(query);
        return R.ok(vos);
    }

    @GetMapping("/{orderNo}")
    @ApiOperation("获取订单详情")
    public R<OrderDetailsVO> getOrderDetail(@PathVariable String orderNo) {
        OrderDetailsVO vo = orderService.getOrderDetail(orderNo);
        return R.ok(vo);
    }

    // 配货
    @PutMapping("/allocated/{id}")
    @ApiOperation("配货")
    @OperLog(title = "订单管理", businessType = BusinessType.UPDATE)
    public R goodsAllocated(@PathVariable Long id) {
        log.info("配货:{}", id);
        orderService.goodsAllocated(id);
        return R.ok();
    }

    @PutMapping("/delivered/{id}")
    @ApiOperation("出库")
    @OperLog(title = "订单管理", businessType = BusinessType.UPDATE)
    public R goodsDelivered(@PathVariable Long id) {
        log.info("出库:{}", id);
        orderService.goodsDelivered(id);
        return R.ok();
    }

    @PutMapping("/cancel")
    @ApiOperation("取消订单")
    @OperLog(title = "订单管理", businessType = BusinessType.UPDATE)
    public R cancelOrder(@RequestBody OrderCancelDTO dto) {
        log.info("取消订单:{}", dto);
        orderService.cancelOrder(dto, true);
        return R.ok();
    }

    @GetMapping("/statistics")
    @ApiOperation("统计各个状态的订单数量")
    public R<List<OrderStatusDTO>> statisticsOrderCount() {
        List<OrderStatusDTO> vos = orderService.statisticsOrderCount();
        return R.ok(vos);
    }


}
